﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class CS_WLLXSX_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(CS_WLLXSX entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.CS_WLLXSXSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(CS_WLLXSX entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_WLLXSX entity2 = context.CS_WLLXSXSet.FirstOrDefault(c => c.WLLXBH == entity.WLLXBH && c.BH == entity.BH);
                if (entity2 != null)
                {
                    entity2.MC = entity.MC;
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string wllxbh, string bh)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_WLLXSX entity = context.CS_WLLXSXSet.FirstOrDefault(c => c.BH == bh && c.WLLXBH == wllxbh);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public CS_WLLXSX Query(string wllxbh, string bh)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_WLLXSX entity = context.CS_WLLXSXSet.FirstOrDefault(c => c.WLLXBH == wllxbh && c.BH == bh);
                return entity;
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <returns>查询结果实体</returns>
        public bool Exist(string wllxbh, string bh, string mc)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_WLLXSX entity = context.CS_WLLXSXSet.FirstOrDefault(c => c.WLLXBH == wllxbh && (c.BH == bh || c.MC == mc));
                return entity != null;
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <returns>查询结果实体</returns>
        public bool Exist(string wllxbh, string mc)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_WLLXSX entity = context.CS_WLLXSXSet.FirstOrDefault(c => c.WLLXBH == wllxbh && c.MC == mc);
                return entity != null;
            }
        }

        /// <summary>
        /// 根据物料类型编号查询属性列表
        /// </summary>
        /// <param name="id">物料类型编号</param>
        /// <returns>查询结果实体</returns>
        public List<CS_WLLXSX> Query(string wllxbh)
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.CS_WLLXSXSet
                        where c.WLLXBH == wllxbh
                        select c;

                List<CS_WLLXSX> list = new List<CS_WLLXSX>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }

                return list;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<CS_WLLXSX> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.CS_WLLXSXSet
                        select c;

                List<CS_WLLXSX> list = new List<CS_WLLXSX>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }
    }
}
